#include"lars_reactor.h"
#include"lars.pb.h"

// 主动给dns发的消息
void on_client_build(net_connection *conn, void *args){
    // 1.组装一个proto文件
    lars::GetRouteRequest requst;

    // 2.设置modid和cmdid
    requst.set_modid(1);
    requst.set_cmdid(1);

    // 3.将proto转成字符串
    std::string requstString;
    requst.SerializeToString(&requstString);

    // 发送数据包
    conn->send_message(requstString.c_str(), requstString.size(), lars::ID_GetRouteRequest);
}


// 处理dns回复的消息
void deal_get_route(const char *data, uint32_t len, int msgid, net_connection* net_conn, void *args){
    // 1.将proto数据进行解包
    lars::GetRouteResponse response;
    response.ParseFromArray(data, len);

    // 2.得到请求的modid和cmdid
    int modId = response.modid();
    int cmdId = response.cmdid();

    // 3.打印结果
    for(int i=0; i<response.host_size(); i++){
        printf("modId:%d, cmdId:%d, host size:%d, ip:%u, port:%d\n", modId, cmdId, response.host_size(), response.host(i).ip(), response.host(i).port());
    }
    printf("-----------------------------------\n");
}

int main(int argc, char** argv){
    event_loop loop;

    // 创建一个客户端
    tcp_client client(&loop, "127.0.0.1", 8888);

    client.set_conn_start(on_client_build);

    client.add_msg_router(lars::ID_GetRouteResponse, deal_get_route);

    loop.event_process();


    return 0;
}